package com.eshop.modules.monitor.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.modules.order.domain.StoreOrderStatus;
import org.slf4j.LoggerFactory;
import java.lang.invoke.SerializedLambda;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import com.eshop.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import java.time.LocalDate;
import com.eshop.logging.service.mapper.LogMapper;
import org.slf4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.monitor.service.VisitsService;
import com.eshop.modules.monitor.domain.Visits;
import com.eshop.modules.monitor.service.mapper.VisitsMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class VisitsServiceImpl extends BaseServiceImpl<VisitsMapper, Visits> implements VisitsService
{
    private static final Logger log;
    private final LogMapper logMapper;
    private final VisitsMapper visitsMapper;
    
    public VisitsServiceImpl(final LogMapper logMapper, final VisitsMapper visitsMapper) {
        this.logMapper = logMapper;
        this.visitsMapper = visitsMapper;
    }
    
    public void save() {
        final LocalDate localDate = LocalDate.now();
        Visits visits = (Visits)this.getOne(Wrappers.<Visits>lambdaQuery().eq(Visits::getDate, localDate.toString()));
        if (visits == null) {
            visits = new Visits();
            visits.setWeekDay(StringUtils.getWeekDay());
            visits.setPvCounts(1L);
            visits.setIpCounts(1L);
            visits.setDate(localDate.toString());
            this.save(visits);
        }
    }
    
    public void count(final HttpServletRequest request) {
        final LocalDate localDate = LocalDate.now();
        Visits visits = (Visits)this.getOne(Wrappers.<Visits>lambdaQuery().eq(Visits::getDate, localDate.toString()));
        if (visits == null) {
            visits = new Visits();
            visits.setPvCounts(1L);
        }
        else {
            visits.setPvCounts(visits.getPvCounts() + 1L);
        }
        final long ipCounts = this.logMapper.findIp(localDate.toString(), localDate.plusDays(1L).toString());
        visits.setIpCounts(ipCounts);
        this.saveOrUpdate(visits);
    }
    
    public Object get() {
        final Map<String, Object> map = new HashMap<String, Object>(4);
        final LocalDate localDate = LocalDate.now();
        final Visits visits = (Visits)this.getOne(Wrappers.<Visits>lambdaQuery().eq(Visits::getDate, localDate.toString()));
        final List<Visits> list = this.visitsMapper.findAllVisits(localDate.minusDays(6L).toString(), localDate.plusDays(1L).toString());
        long recentVisits = 0L;
        long recentIp = 0L;
        for (final Visits data : list) {
            recentVisits += data.getPvCounts();
            recentIp += data.getIpCounts();
        }
        map.put("newVisits", visits.getPvCounts());
        map.put("newIp", visits.getIpCounts());
        map.put("recentVisits", recentVisits);
        map.put("recentIp", recentIp);
        return map;
    }
    
    public Object getChartData() {
        final Map<String, Object> map = new HashMap<String, Object>(3);
        return map;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)VisitsServiceImpl.class);
    }
}
